﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using Autodesk.Revit.ApplicationServices;
using Autodesk.Revit.Attributes;
using Autodesk.Revit.DB;
using Autodesk.Revit.Exceptions;
using Autodesk.Revit.UI;
using Autodesk.Revit.UI.Selection;
using YArchitech.Controls;
using YArchitech.LIB;
using YArchitech.Revit;

namespace YArchitech.Plumbing
{
	[Transaction(TransactionMode.Manual)]
	[Regeneration(RegenerationOption.Manual)]
	public class DuctsAccessoryTag : IExternalCommand
	{
		public Result Execute(ExternalCommandData commandData, ref string message, ElementSet elements)
		{
			Transaction transaction = null;
			UIApplication application = commandData.Application;
			Autodesk.Revit.ApplicationServices.Application application2 = application.Application;
			Document document = application.ActiveUIDocument.Document;
			UIDocument activeUIDocument = commandData.Application.ActiveUIDocument;
			Autodesk.Revit.DB.View activeView = document.ActiveView;
			Selection selection = activeUIDocument.Selection;
			if (!YJKRevitTools.ViewCheck(activeUIDocument, new List<ViewType>
			{
				(ViewType)1,
				(ViewType)2,
				(ViewType)4
			}))
			{
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			activeView = document.ActiveView;
			if (activeView is View3D && !(activeView as Autodesk.Revit.DB.View3D).IsLocked)
			{
				YJKMessageBox.Warning("该三维视图无法标注，请锁定视角后再进行此操作！");
				return Autodesk.Revit.UI.Result.Cancelled;
			}
			Result result;
			try
			{
				transaction = new Transaction(document, "风管阀件标注");
				transaction.Start();
				for (;;)
				{
					ISelectionFilter selectionFilter = new DuctAccessoryFilter();
					Reference reference = selection.PickObject(Autodesk.Revit.UI.Selection.ObjectType.Element, selectionFilter, "请选择要标注的风管阀件");
					Element element = document.GetElement(reference);
					XYZ xyz = selection.PickPoint("请选择标注位置的点");
					YJKRevitTools.DeleteTagOfElement(document, BuiltInCategory.OST_DuctAccessoryTags, element);
					FamilySymbol familySymbol = YJKRevitTools.GetFamilySymbol(document, "HY_风管阀件标记族", BuiltInCategory.OST_DuctAccessoryTags);
					if (familySymbol != null)
					{
						IndependentTag independentTag = document.Create.NewTag(activeView, element, false, 0, 0, xyz);
						independentTag.ChangeTypeId(new ElementId(familySymbol.Id.IntegerValue));
						independentTag.HasLeader = true;
					}
					else if (familySymbol == null)
					{
						string text = Path.Combine(Product.FamilyLocation, RevitVersion.GetVersionNumber(document), "MarkTag", "HY_风管阀件标记族.rfa");
						document.LoadFamily(text);
						document.Create.NewTag(activeView, element, false, 0, 0, xyz).HasLeader = true;
					}
				}
			}
			catch (Autodesk.Revit.Exceptions.OperationCanceledException)
			{
				result = Autodesk.Revit.UI.Result.Succeeded;
			}
			catch (Exception ex)
			{
				MessageBox.Show(ex.Message, "盈建科提示", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
				result = Autodesk.Revit.UI.Result.Failed;
			}
			finally
			{
				if (transaction != null && (int)transaction.GetStatus() == 1)
				{
					transaction.Commit();
				}
				if (transaction != null && (int)transaction.GetStatus() != 3)
				{
					transaction.RollBack();
				}
			}
			return result;
		}
	}
}
